package com.easyadmin.common.aop;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.easyadmin.modules.stock.domain.SysStockPriceWarnEntity;
import com.easyadmin.modules.stock.domain.SysStocksDayPrice;
import com.easyadmin.modules.stock.domain.SysTradeDateEntity;
import com.easyadmin.modules.stock.mapper.SysStockPriceWarnDao;
import com.easyadmin.modules.stock.mapper.SysStocksDayPriceDao;
import com.easyadmin.modules.stock.mapper.SysTradeDateDao;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.easyadmin.common.tools.SendMsg;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *
 *   定时任务
 *   1.每日数据导入
 *   2.发送钉钉
 *   3.自动跑模型和规则
 *   4.自动生成报表
 *   5.自动提示重点监控的价格和信息内容
 *
 *
 */
@Component
@Slf4j
public class indexJob {

       @Autowired
       SysTradeDateDao sysTradeDateDao;
       @Autowired
       private SysStocksDayPriceDao sysStocksDayPriceDao;
       @Autowired
       SysStockPriceWarnDao sysStockPriceWarnDao;


       // 期货每天早上八点和晚上八点各执行一次
       @Scheduled(cron = "0 50 8,20 * * ?")
       public  void  futureDayPrice(){
              Date date = new Date();
              SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
              String dateString = sdf.format(date);
              SysTradeDateEntity data =sysTradeDateDao.selectOne(new QueryWrapper<SysTradeDateEntity>()
                      .lambda().eq(SysTradeDateEntity::getSysDate,dateString));
              log.info("开始执行定时任务");
              if(data!=null){
                     String currentPath = System.getProperty("user.dir");
                     if(StringUtils.isNotBlank(data.getTradDayFlag()) && data.getTradDayFlag().equals("1")){
                            log.info("执行python ");
                            try {
                                   // 调用Python脚本
                                   Process p = Runtime.getRuntime().exec("python "+currentPath+"\\src\\main\\resources\\python\\data_store.py");
                                   //SendMsg.SendDingDing("今日执行完成");
                                   // 获取脚本执行的结
                                   BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
                                   String line;
                                   while ((line = in.readLine()) != null) {
                                          System.out.println(line);
                                   }
                                   in.close();
                            } catch (Exception e) {
                                   e.printStackTrace();
                            }
                     }
              }
       }
       /**
        * 1.股票数据入库
        * 2.先跑语句
        * 3。
        *
        *
        *
        */
       @Scheduled(cron = "0 50 20,21 * * ?")
       public  void  stockDayPrice(){
              Date date = new Date();
              SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
              String dateString = sdf.format(date);
              SysTradeDateEntity data =sysTradeDateDao.selectOne(new QueryWrapper<SysTradeDateEntity>()
                      .lambda().eq(SysTradeDateEntity::getSysDate,dateString));
              log.info("开始执行定时任务stockDayPrice");
              if(data!=null){
                     String currentPath = System.getProperty("user.dir");
                     if(StringUtils.isNotBlank(data.getTradDayFlag()) && data.getTradDayFlag().equals("1")){
                            log.info("执行python  stockDayPrice");
                            try {
                                   // 调用Python脚本
                                   Process p = Runtime.getRuntime().exec("python "+currentPath+"\\src\\main\\resources\\python\\stock_store.py");
                                   //SendMsg.SendDingDing("今日执行完成");
                                   // 获取脚本执行的结
                                   BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
                                   String line;
                                   while ((line = in.readLine()) != null) {
                                          System.out.println(line);
                                   }
                                   in.close();
                            } catch (Exception e) {
                                   e.printStackTrace();
                            }
                     }

                     //先跑语句，当前收盘价格低于预警价格,有数据插入到审批表
                     List<SysStocksDayPrice> list= sysStocksDayPriceDao.selectDateList(dateString);
                     if(list.size()>0){
                            //查询重点监控的数据
                            for(int i=0;i<list.size();i++){
                                   SysStocksDayPrice sysStocksDayPrice=list.get(i);
                                   SysStockPriceWarnEntity warnEntity=new SysStockPriceWarnEntity();
                                   warnEntity.setProId(sysStocksDayPrice.getStockCode());
                                   warnEntity.setProName(sysStocksDayPrice.getStockName());
                                   warnEntity.setAuditStatus("0");
                                   warnEntity.setAuditTime(new Date());
                                   warnEntity.setWarnDesc("价值高，可以试点参与");
                                   sysStockPriceWarnDao.insert(warnEntity);
                            }
                     }



              }
       }

}
